﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MedManager.BLL;
using MedManager.Model;
using MedManager.Src;
using Maticsoft.DBUtility;

namespace MedManager
{
    public partial class Drug : Form
    {
        public Drug()
        {
            InitializeComponent();
        }

        private string drugName;

        public string DrugName
        {
            set { drugName = value; }
            get { return drugName; }
        }

        /// <summary>
        /// 检查药品名称是否存在
        /// </summary>
        /// <param name="drugName">药品名称</param>
        /// <returns>存在则返回false 不存在则返回true</returns>
        private bool CheckDrugName(string drugName)
        {
            bool flag = true;
            string sql = "select * from medInfo where name=@name";
            SqlParameter[] sqlParameters = new SqlParameter[1];
            sqlParameters[0] = new SqlParameter("@name", drugName);
            DataSet ds = DbHelperSQL.Query(sql, sqlParameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                flag = false;
            }
            return flag;
        }

        private void DoSave()
        {
            if (!String.IsNullOrEmpty(txtDrugName.Text))
            {
                string drugName = txtDrugName.Text.Trim();
                if (CheckDrugName(drugName) == true)
                {
                    medInfoEntity medInfo = new medInfoEntity();
                    medInfo.name = drugName;
                    medInfo.price = 0;
                    int result = Func.medInfoBll.Add(medInfo);
                    if (result != 0)
                    {
                        MessageBox.Show("添加成功！");
                        txtDrugName.Text = "";

                    }
                    else
                    {
                        MessageBox.Show("添加失败！");
                    }
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DoSave();

        }

        private void Drug_Load(object sender, EventArgs e)
        {
            this.Text = "药品添加";
            if (!String.IsNullOrEmpty(drugName))
            {
                txtDrugName.Text = drugName;
            }
        }

        private void txtDrugName_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                DoSave();
            }
        }
    }
}
